package com.xiang.adapter;

import java.util.List;

import com.xiang.constant.DistrictsBaseinfo;
import com.xiang.seemovies.R;
import com.xiang.utils.sharedpreferencesUtil;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class GetRegionAdapter extends BaseAdapter{
	 private List<DistrictsBaseinfo> districtsList;
	 private LayoutInflater inflater;
	 private sharedpreferencesUtil util;
	public GetRegionAdapter(	Context context, List<DistrictsBaseinfo> list) {
		districtsList = list;
		if(inflater ==null){
			inflater = LayoutInflater.from(context);
		}
		if(util ==null){
			util = new sharedpreferencesUtil(context);
		}
	}
	public void resetData( List<DistrictsBaseinfo> list) {
		this.districtsList = list;
	}
	@Override
	public int getCount() {
		return districtsList.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		DistrictsBaseinfo districtsBaseinfo = districtsList.get(position);
		ViewHolder holder = null;
		if(convertView==null){
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.city_adapter_layout, null);
			holder.name = (TextView) convertView.findViewById(R.id.txv_city_name);
			holder.check = (CheckBox) convertView.findViewById(R.id.ckb_city_check);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		holder.name.setText(districtsBaseinfo.district_name);
		String localSaveCityName = util.getRegion();
		if(localSaveCityName!=null && districtsBaseinfo.district_name.equals(localSaveCityName)){
			holder.check.setVisibility(View.VISIBLE);
			holder.check.setChecked(true);
		}else{
			holder.check.setVisibility(View.GONE);
			holder.check.setChecked(false);
		}
		return convertView;
	}
	class ViewHolder{
		TextView name;
		CheckBox check;
	}
}
